package com.vv.tools.common.demo;

import com.vv.tools.common.util.FileUtils;
import com.vv.tools.common.util.JsonUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

public class ThCarDemo {

    public static void main(String[] args) {
        ThCarDemo thCarDemo = new ThCarDemo();
        thCarDemo.run();
    }

    private static long printValue(long maxId, String key, String subKey, String value, boolean isLast) {
        String sql = "\t (%s,'Chubb_TH',941307999536284,'1','%s','vehicleModel','%s','%s','%s','-1','-1','2025-04-21 11:28:54','2025-04-21 " +
                     "11:30:31','N','lazada')%s\n";
        System.out.printf(sql, ++maxId, key, subKey, key, value.replaceAll("\\s+", "").trim(), isLast ? ";" : ",");
        return maxId;
    }

    private void run() {
        // 1. chubb API 车型库
        chubbApiData(true);

        // 2. chubb bifrost mapping
        chubbMapping(false);

    }

    private void chubbMapping(boolean skip) {
        if (skip) {
            return;
        }
        long maxId = 862668936562478L;
        String motorStr = FileUtils.read(Objects.requireNonNull(FileUtils.getClassPathFile("demo/th_motor.txt")));
        String[] split = motorStr.split("\n");
        String insertSql = "INSERT INTO lazada_bifrost_00.product_dict_mapping (id,insurance_code,sales_goods_id,insurance_product_id,item_name," +
                           "dict_type,sub_dict_type,dict_value,mapping_value,creator,modifier,gmt_created,gmt_modified,is_deleted,tenant_code) " +
                           "VALUES";
        System.out.println();
        int count = 0;
        int size = 100;
        for (String s : split) {
            if (count % size == 0) {
                System.out.println(insertSql);
            }
            String[] ss = s.split("\t");
            String key = ss[0] + " / " + ss[1] + " / " + ss[2] + " / " + ss[3];
            maxId = printValue(maxId, key, "BrandCode", ss[5], false);
            maxId = printValue(maxId, key, "ModelCode", ss[6], false);
            maxId = printValue(maxId, key, "ModelYear", ss[7], false);
            maxId = printValue(maxId, key, "VoluntaryCode", ss[8], false);
            maxId = printValue(maxId, key, "SeatCapacity", ss[9], false);
            maxId = printValue(maxId, key, "GVWCapacity", ss[10], false);
            maxId = printValue(maxId, key, "EngineSize", ss[11], (count + 1) % size == 0 || (count + 1) == split.length);
            count++;
        }
    }

    private void chubbApiData(boolean skip) {
        if (skip) {
            return;
        }
        String apiTxt = readFile("ThCar.txt");
        String[] split = apiTxt.split("\n");
        Map<String, Map<String, String>> map = JsonUtil.toObject(readFile("ThCarVehicleKey.Json"), Map.class);
        for (String s : split) {
            String[] ss = s.split("\t");
            String key = ss[0] + "_" + ss[1].replace(" ", "") + "_" + ss[2] + "_" + ss[3];
            String desc = ss[4];
            String[] dd = desc.split(" ");
            AtomicReference<String> vehicleKey = new AtomicReference<>("");
            assert map != null;
            if (map.containsKey(key)) {
                Map<String, String> mm = map.get(key);
                mm.forEach((k, v) -> {
                    boolean flag = true;
                    for (String d : dd) {
                        String replace = d.replace("(", "").replace(")", "");
                        if (!v.toUpperCase().contains(replace.toUpperCase())) {
                            flag = false;
                        }
                    }
                    if (flag && "".equals(vehicleKey.get())) {
                        vehicleKey.set(k);
                    }
                });
                mm.remove(vehicleKey.get());
            }
            System.out.println(vehicleKey.get());
        }
    }

    private void thCarVehicleKeyJson() {
        String apiTxt = readFile("ThCarApi.txt");
        String[] split = apiTxt.split("\n");
        Map<String, Map<String, String>> map = new HashMap<>();
        for (String s : split) {
            String[] ss = s.split("\t");
            String key = ss[0] + "_" + ss[1] + "_" + ss[2] + "_" + ss[4];
            String vehicleKey = ss[3];
            String desc = ss[5];
            if (map.containsKey(key)) {
                map.get(key).put(vehicleKey, desc);
                continue;
            }
            Map<String, String> mm = new HashMap<>();
            mm.put(vehicleKey, desc);
            map.put(key, mm);
        }
        writeFile("ThCarVehicleKey.Json", JsonUtil.toJsonString(map));
    }


    private void writeFile(String fileName, String content) {
        File classPathFile = FileUtils.getClassPathFile("classpath:demo/" + fileName);
        assert classPathFile != null;
        FileUtils.write(classPathFile, content);
    }


    private String readFile(String fileName) {
        File classPathFile = FileUtils.getClassPathFile("classpath:demo/" + fileName);
        assert classPathFile != null;
        return FileUtils.read(classPathFile);
    }

}
